<?php
/**
 * Created by PhpStorm.
 * User: zyark
 * Date: 2017/5/30
 * Time: 20:47
 */

namespace Home\Logic;

use Home\Model\Page;
use Home\Logic\MoneyLogLogic;
class UserLogic
{
    /**
     *创建用户并绑定上级
     * @param $openId  微信openid
     * @param $user_id 上级id
     * @return array $user 用户信息
     */
    public function bindSuperior($openId,$user_id){
        $model = D('Users');
        $user = $model->where(array('openid'=>':openid'))->bind(array(':openid'=>$openId))->find();
        if($user){
            return $user;
        }
        $superior = $model->where(array('user_id'=>':user_id'))->bind(array(':user_id'=>$user_id))->find();
        $moneyLogic = new MoneyLogLogic();
        //获取绑定上级时的返利金额
        $bindUserRebate = M('Config')->where(array('name'=> 'bind_user_rebate'))->getField('value');
        //获取绑定上级时下级返利金额
        $bindUserRebateDown = M('Config')->where(array('name'=> 'bind_user_rebate_down'))->getField('value');
        $user['first_leader'] = $superior['user_id'];
        $user['second_leader'] = $superior['first_leader'];
        $user['third_leader'] = $superior['second_leader'];
        $user['openid'] = $openId.'';
        $user['rebate_money'] = $bindUserRebateDown;
        $user_id = $model->add($user);
        $user['user_id'] = $user_id;
        //给上级
        $superior['rebate_money'] += $bindUserRebate;
        M('users')->save($superior);
        //添加到money_log记录表中
        $moneyLogic->addMoneyLog($superior['user_id'], $user['user_id'], 1, 0, $bindUserRebate, $superior['rebate_money']+$superior['user_money'], '绑定上下级上级返利');
        $moneyLogic->addMoneyLog($user['user_id'], $superior['user_id'], 1, 0, $bindUserRebateDown, $user['rebate_money'], '绑定上下级下级返利');
        return $user;
    }


    //获取上级用户信息
    public function getLastUser($user_id){

        $lastUserId = M('users')->where(array('user_id'=>$user_id))->getField('first_leader');
        if($lastUserId)
            $lastUser = M('users')->where(array('user_id'=>$lastUserId))->find();
        else
            return false;
        return  $lastUser;
    }



    //返利给上一级用户
    public function rebateLastUser($money, $user_id){
        $moneyLogic = new MoneyLogLogic();
        $lastUser = $this->getLastUser($user_id);
        if($lastUser){
            $user = M('users')->where(array('user_id'=>$user_id))->find();
            $lastUser['user_money'] += $money;
            M('users')->where(array('user_id'=>$lastUser['user_id']))->save($lastUser);
            $lastUserMoney = M('users')->where(array('user_id'=>$lastUser['user_id']))->getField('user_money');
            $moneyLogic->addMoneyLog($lastUser['user_id'], $user['user_id'], 1, 0,  $money, $lastUserMoney, $user['nickname'] . '(返利)');
        }
        return true;
    }

    //返利包装

    public function  rebate( $order_id){
        $order = M('order')->where(array('order_id'=>$order_id))->find();
        $user = M('users')->where(array('user_id'=>$order['user_id']))->find();
//        $userLogic = new UserLogic();
        $moneyLogic = new MoneyLogLogic();
        //获取返利比例
        $rebate = M('config')->where(array('id' => 73))->getField('value');
        $lastUser = $this->getLastUser($user['user_id']);
        if ($lastUser) {
            $this->rebateLastUser((double)$rebate * $order['order_amount'], $user['user_id']);
            $lastUserMoney = M('users')->where(array('user_id'=>$lastUser['user_id']))->getField('user_money');
            //记录到moneyLog表中需要记录两条数据一条当前用户支出一条上级用户收入返利
//            ($user_id, $score_id, $type, $put_type, $money, $current_money,$remark)
            //获取上级用户
            $moneyLogic->addMoneyLog($lastUser['user_id'], $user['user_id'], 1, 0, $order['order_amount'] * $rebate, $lastUserMoney, $user['nickname'] . '(返利)');
        }

    }

    /**
     * 获取用户下级
     * @param $user_id 用户id
     * @param $pagenum 页数
     * @param $pagesize 页号
     * @return array 用户下级列表
     */
    public function getLastOneList($user_id,$pagenum,$pagesize){
        $startno = ($pagenum - 1) * $pagesize;
//        $limit = $startno . ',' . $pagesize;//每页的数据数和内容$limit
        $map['first_leader'] = ':first_leader';
        $bind[':first_leader'] = $user_id;
        $userList = D('Users')
            ->where($map)->bind($bind)
//            ->limit($limit)
            ->field('head_pic,nickname,user_id,reg_time')
            ->order('user_id desc')
//            ->limit($limit)
            ->select();
        foreach ($userList as $key => $value){
            $userList[$key]['reg_time'] = date('Y-m-d m:s',$value['reg_time']);
        }
        $data['list'] = $userList;
        $count = D('Users')->where($map)->bind($bind)->count();
        $page = new Page($pagenum, $count, $pagesize);
        $page = (array)$page;//转换为数组
        //分页数据
//        $page['cpage'] = $pagenum;//当前页面
//        $page['totalnum'] = floor(($count + $pagesize - 1) / $pagesize);
        $data['page'] = $page;
        return $data;
    }
    /**
     * 将用户设置为代理商
     * @param $user_id 用户id
     */
    public function agentByUserId($user_id, $money){
        $model = M('Users');
        //获取用户
        $user = $model->where(array('user_id'=> $user_id))->find();
        $lowMoney = M('config')->where(array('name'=> 'agent_recharge'))->getField('value');
        if($user){
            //如果该次充值金额达到代理商最低金额则设置用户类型为代理商若不是则只将钱添加到rebate_money
            if($lowMoney <= $money)
                $user['user_type']  = 1; //设置用户类型
            $user['rebate_money'] += $money;
            $model->save($user);
        }
    }
    /**
     * 获取用户下级代理商关系数组
     * @param $user_id 用户id
     * @return array 用户下级列表
     */
    public function  getDownUserInfo($user_id){
        $result = array();
        $son = M('Users')->where(array('first_leader'=>$user_id, 'user_type'=>1))->select();
        if(!$son){
            $result[$user_id]['son'] = $son;
            $sum_sales = M('agent_log')->where(array('user_id'=> $user_id))->getField('sum(money)');
            if(!$sum_sales)
                $sum_sales = 0;
            $result[$user_id]['sum_sales'] = $sum_sales;
        }
        else{

            foreach ($son as $key => $val){
                $result[$user_id]['son'][] = $this->getDownUserInfo($val['user_id']);

            }

        }


        return  $result;
    }


}